package com.imooc.bilibili.api;

import com.imooc.bilibili.api.support.UserSupport;
import com.imooc.bilibili.domain.JsonResponse;
import com.imooc.bilibili.domain.UserFollowing;
import com.imooc.bilibili.domain.UserFollowingGroup;
import com.imooc.bilibili.service.UserFollowingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserFollowingApi {

    @Autowired
    private UserFollowingService userFollowingService;

    @Autowired
    private UserSupport userSupport;

    @PostMapping("/user-followings")
    public JsonResponse<String> addUserFollowings(@RequestBody UserFollowing userFollowing) {

         Long userId = userSupport.getCurrentUserId();
         userFollowing.setUserId(userId);
         userFollowingService.addUserFollowings(userFollowing);
        return JsonResponse.success();
     }

    @GetMapping("/user-followings")
    public JsonResponse<List<UserFollowingGroup>> getUserFollowings() {
         Long userId = userSupport.getCurrentUserId();
         List<UserFollowingGroup> userFollowings = userFollowingService.getUserFollowings(userId);
         return new JsonResponse<>(userFollowings);
     }

    @GetMapping("/get-fans")
    public JsonResponse<List<UserFollowing>> getUserFans() {
        Long userId = userSupport.getCurrentUserId();
        List<UserFollowing> userFans = userFollowingService.getUserFans(userId);
        return new JsonResponse<>(userFans);
    }

    @PostMapping("/user-following-groups")
    public JsonResponse<Long> addUserFollowingGroups(@RequestBody UserFollowingGroup userFollowingGroup) {

        Long userId = userSupport.getCurrentUserId();
        userFollowingGroup.setUserId(userId);
        Long groupId = userFollowingService.addUserFollowGroups(userFollowingGroup);
        return new JsonResponse<>(groupId);
    }

    @GetMapping("/user-following-groups")
    public JsonResponse<List<UserFollowingGroup>> getUserFollowingGroups() {

        Long userId = userSupport.getCurrentUserId();
        List<UserFollowingGroup> userFollowingGroupList = userFollowingService.getUserFollowingGroups(userId);
        return new JsonResponse<>(userFollowingGroupList);
    }
}
